import 'package:f_enjoy_plus/components/Header.dart';
import 'package:f_enjoy_plus/components/HouseTatusTag.dart';
import 'package:f_enjoy_plus/core/constants/app_colors.dart';
import 'package:f_enjoy_plus/utils/RouteObserver.dart';
import 'package:f_enjoy_plus/utils/http.dart';
import 'package:f_enjoy_plus/utils/toast.dart';
import 'package:flutter/material.dart';

class HouseList extends StatefulWidget {
  const HouseList({super.key});
  @override
  State<HouseList> createState() => _HouseListState();
}

class _HouseListState extends State<HouseList> with RouteAware {
  List houseList = [];
  bool isLoading = false;

  @override
  void initState() {
    super.initState();
    getHouseList();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    routeObserver.subscribe(this, ModalRoute.of(context)! as PageRoute);
  }

  @override
  void dispose() {
    routeObserver.unsubscribe(this);
    super.dispose();
  }

  @override
  void didPopNext() {
    super.didPopNext();
    getHouseList();
  }

  getHouseList() async {
    try {
      isLoading = true;
      final res = await http.get('/room');
      print(res);
      setState(() {
        houseList = res;
      });
    } catch (e) {
    } finally {
      isLoading = false;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Header(title: '我的房屋'),
      body:
          isLoading
              ? Center(child: CircularProgressIndicator())
              : Container(
                padding: EdgeInsets.all(16),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  spacing: 16,
                  children: [
                    Expanded(
                      child: ListView.builder(
                        itemCount: houseList.length,
                        itemBuilder: (context, index) {
                          return GestureDetector(
                            onTap: () {
                              Navigator.pushNamed(
                                context,
                                '/houseDetail',
                                arguments: {'id': houseList[index]['id']},
                              );
                            },
                            child: Container(
                              padding: EdgeInsets.all(12),
                              margin: EdgeInsets.only(bottom: 16),
                              decoration: BoxDecoration(
                                color: AppColors.white,
                                borderRadius: BorderRadius.circular(8),
                              ),
                              child: Column(
                                spacing: 8,
                                children: [
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      // 使用 RoomStatus Map 通过 status 字段获取对应的中文状态
                                      Text(houseList[index]['point'] ?? ''),
                                      HouseStatusTag(
                                        status:
                                            houseList[index]['status'] ?? 0,
                                      ),
                                    ],
                                  ),
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text('房间号'),
                                      Text(
                                        "${houseList[index]['building']} - ${houseList[index]['room']}",
                                      ),
                                    ],
                                  ),
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text('业主'),
                                      Text(houseList[index]['name'] ?? ''),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          );
                        },
                      ),
                    ),
                    ElevatedButton(
                      onPressed: () {
                        if (houseList.length >= 5) {
                          ToastUtils.error('最多添加5套房屋');
                          return;
                        }
                        Navigator.pushNamed(context, '/location');
                      },
                      child: SizedBox(
                        height: 48,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(Icons.add, size: 24),
                            Text('添加房屋', style: TextStyle(fontSize: 16)),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
    );
  }
}